/**
 * 红心频道功能模块
 */
(function() {
    'use strict';

    CCR.Models.Favorites = Backbone.Model.extend({
    	file_url:"favorite/favorites.txt",
    	full_number:200,
    	channel_name:"favorite",
    	//添加收藏歌曲 (歌曲id)
    	addFavSong:function(song_id,callback){
    		var self = this;
    		self.isFull(song_id, function(full){
    			if(!full){
    				sdk.io.readJSONFile(self.file_url, false, function(data){
    	    			//add id
    					if(!isArray(data)){
    						data = new Array();
    					}
    					data.push({songid:song_id});
    	    			sdk.io.writeJSONFile(self.file_url, data, false);
    	    			typeof callback ==="function"&&callback();
    	    		});
    			}
    		});
    	},
    	//取消收藏歌曲(歌曲id)
    	deleteFavSong:function(song_id,callback){
    		var self = this;
    		sdk.io.readJSONFile(self.file_url, false, function(data){
    			var deleted_data = new Array();
    			for(var i = 0; i < data.length; i++){
    				if(data[i].songid!==song_id){
    					deleted_data.push(data[i]);
    				}
    			}
    			sdk.io.writeJSONFile(self.file_url, deleted_data, false);
    			typeof callback ==="function"&&callback();
    		});
    	},
    	//判断是否已收藏(歌曲id)
    	isFavorite:function(song_id,callback){
    		var self = this;
    		sdk.io.readJSONFile(self.file_url, false, function(data){
    			var is = false;
    			for(var i = 0; i < data.length; i++){
    				if(data[i].songid===song_id){
    					is = true;
    				}
    			}
    			typeof callback ==="function"&&callback(is);
    		});
    	},	
    	//是否已达到收藏上限
    	isFull:function(song_id,callback){
    		var self = this;
    		sdk.io.readJSONFile(self.file_url, false, function(data){
    			var full = false;
    			if(data.length >= self.full_number){
    				full = true;
    				log("已到达上限"+self.full_number);
    			}
    			typeof callback ==="function"&&callback(full);
    		});
    	},
    	//获取红星频道歌曲
    	getFavSong:function(callback){
    		var self = this;
    		sdk.io.readJSONFile(self.file_url, false, function(data){
    			var is = false;
    			if(!isArray(data)){
    				data = new Array();
    			}
    			typeof callback ==="function"&&callback(data);
    		});
    		
    	},
    	// 在频道列表中添加红心频道
    	addFavChannel:function(channel_list){
    		var self = this;
    		channel_list.push(self.channel_name);
    		return channel_list;
    	}
    });
    
})();